#ifndef _KEY_RCMDER_H_
#define _KEY_RCMDER_H_

#include "Dictionary.h"
#include "TcpConnection.h"
#include <memory>
#include <queue>
#include <set>
#include <string>

using std::priority_queue;
using std::set;
using std::shared_ptr;
using std::string;

struct CandidateResult {
    string _word;
    int _freq;
    int _dist;
};

struct MyCompare {
    bool operator()(const CandidateResult &lhs, const CandidateResult &rhs);
};

using TcpConnectionPtr = shared_ptr<TcpConnection>;

class KeyRecommander {
  public:
    KeyRecommander(string &query, const TcpConnectionPtr &, Dictionary *);
    void execute();                         // 执行查询
    void queryIndexTable();                 // 查询索引
    void statistic(set<int> &, set<int> &); // 进行计算
    int distance(const string &rhs);        // 计算最小编辑距离
    void response();                        // 响应客户端的请求

  private:
    string _queryWord;                                                              // 传入的字符串
    TcpConnectionPtr _conn;                                                         // 与客户端通信的Tcp连接
    Dictionary *_dict;                                                              // 字典的指针
    priority_queue<CandidateResult, vector<CandidateResult>, MyCompare> _resultQue; // 查询结果
};

#endif